<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command\kuaishou\schema;

use app\command\BaseWork;
use app\service\batch\KuaiShouBatchService;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

class KuaiShouSchemaUseWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('kuaishou_schema_use');
        // 设置描述
        $this->setDescription('直达链接使用');
        $this->addArgument('type', Argument::REQUIRED, '类型');
    }

    /**
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        $this->getOutputMessage('直达链接使用开始');
        $type = intval($input->getArgument('type'));

        // 超过5min就停止
        $t = time();
        $runTime = 5 * 60;
        while (time() - $t <= $runTime) {
            try {
                if (! KuaiShouBatchService::batchSchemaUse($type)) {
                    $this->getOutputMessage(KuaiShouBatchService::getError(0));
                    sleep(rand(5, 10));
                    continue;
                }
            } catch (\Exception $e) {
                $this->getOutputMessage($e->getMessage());
                $this->getOutputMessage($e->getFile() . '[' . $e->getLine() . ']');
                $this->getOutputMessage($e->getTraceAsString());
            }
        }

        $this->getOutputMessage('直达链接使用结束');
    }
}
